<?php
/**
 * @package   auto_occasion
 * @subpackage 
 * @author    CMS
 * @copyright  2011
 * @license    All rights reserved
 */

class concurrenceSrv {

     /**
     * Fonction permettant de sauvegarder les concurrences d'un essai
     * @return object $concurrence
     *
     */
    public function ajouter ($concurrence) {
       
        // instanciation de la factory
        $maFactory = jDao::get("commun~ao_essais_concurrence");
        $records   = jDao::createRecord("commun~ao_essais_concurrence");

        $records->concur_name           = $concurrence['concur_name'];
        $records->concur_nbetoiles      = $concurrence['concur_nbetoiles'];
        $records->concur_position       = $concurrence['concur_position'];
        $records->concur_marque_id      = $concurrence['concur_marque_id'];
        $records->concur_modele_id      = $concurrence['concur_modele_id'];
        $records->concur_essai_id       = $concurrence['iId'];

         if ($maFactory->insert($records)) {
            return $records->chap_id;
        }

    }
     /**
     * Fonction permettant de modifier les concurrences d'un essai
     * @return object $partenaire
     *
     */
    public function modifier ($concurrence) {
        // instanciation de la factory
        $maFactory = jDao::get("commun~ao_essais_concurrence");
        $records   = $maFactory->get($concurrence['concur_id']);

        $records->concur_name           = $concurrence['concur_name'];
        $records->concur_nbetoiles      = $concurrence['concur_nbetoiles'];
        $records->concur_position       = $concurrence['concur_position'];
        $records->concur_marque_id      = $concurrence['concur_marque_id'];
        $records->concur_modele_id      = $concurrence['concur_modele_id'];
        $records->concur_essai_id       = $concurrence['iId'];

        if ($maFactory->update($records)) {
            return $concurrence['concur_id'];
        }

    }


    /**
     * Bloc left : récupération des concurrences
     * @param $_bIsConcurenceValid :paramètre necessaire pour l'affichage des concurences qui a pour marqueId non null et modele_id non null
     * @return 
     */
     public function loadConcurrence($essaiId = 0 ,$_bIsConcurenceValid = false ) {

         $cnx     = jDb::getConnection();

         $zQuery  = " SELECT concur_name, concur_id, concur_essai_id, concur_nbetoiles, concur_marque_id, IF(concur_modele_id IS NULL,0,concur_modele_id) AS concur_modele_id, (SELECT E2.ess_id FROM ao_essais E2 WHERE E2.ess_iMarqueId = concur_marque_id AND E2.ess_iModelId = concur_modele_id AND E2.ess_id <> $essaiId limit 1) AS essaiAutreId FROM ao_essais_concurrence EC, ao_essais E ";
         $zQuery .= " WHERE EC.concur_essai_id = E.ess_id";
         $zQuery .= " AND EC.concur_essai_id=".$essaiId ;
		 if( $_bIsConcurenceValid == true )
		 {
			$zQuery .= " AND EC.concur_marque_id IS NOT NULL  " ;
			$zQuery .= " AND ( SELECT COUNT(ess.ess_id) FROM ao_essais AS ess WHERE ess.ess_iMarqueId = concur_marque_id AND (ess.ess_iModelId = concur_modele_id OR ess.ess_iModelId IS NULL OR ess.ess_iModelId = 0)) > 0  " ;
		 }
		 $zQuery .= " ORDER BY EC.concur_position";
         $jdb = jDb::getDbWidget();

         $return = $jdb->fetchAll($zQuery);
         return $return;
     }

    /**
     * Renvoie tous les fiches d'une Categorie
     * @param array $_toParams
     * @return array 
     */
    public static function reOrder ($_toParams) {
        
        $oFac = jDao::get('commun~ao_essais_concurrence') ;
        
        $tiMarqueIds = array_key_exists('tiMarqueIds', $_toParams) ? $_toParams['tiMarqueIds'] : array() ;

        foreach ($tiMarqueIds as $iKey => $iMarqueId) {
            $oRec = $oFac->get($iMarqueId) ;
            $oRec->concur_position = $iKey ;
            $oFac->update($oRec) ;
        }
    }


}